package com.alibaba.datax.plugin.reader.mongodbreader.util;

import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.plugin.reader.mongodbreader.MongoDBReaderErrorCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

import java.util.Objects;

/**
 * @author cuijianpeng
 * @desc FastJson工具类
 * @date 2020/6/25 10:21
 */
public class FastJsonUtil {

    /**
     * FastJson Feature
     *
     * @param queryFeature FastJson Feature字符串
     * @return Feature
     */
    public static Feature getJsonFeature(String queryFeature) {
        switch (queryFeature) {
            case "AutoCloseSource":
                return Feature.AutoCloseSource;
            case "AllowComment":
                return Feature.AllowComment;
            case "AllowUnQuotedFieldNames":
                return Feature.AllowUnQuotedFieldNames;
            case "AllowSingleQuotes":
                return Feature.AllowSingleQuotes;
            case "InternFieldNames":
                return Feature.InternFieldNames;
            case "AllowISO8601DateFormat":
                return Feature.AllowISO8601DateFormat;
            case "AllowArbitraryCommas":
                return Feature.AllowArbitraryCommas;
            case "UseBigDecimal":
                return Feature.UseBigDecimal;
            case "IgnoreNotMatch":
                return Feature.IgnoreNotMatch;
            case "SortFeidFastMatch":
                return Feature.SortFeidFastMatch;
            case "DisableASM":
                return Feature.DisableASM;
            case "DisableCircularReferenceDetect":
                return Feature.DisableCircularReferenceDetect;
            case "InitStringFieldAsEmpty":
                return Feature.InitStringFieldAsEmpty;
            case "SupportArrayToBean":
                return Feature.SupportArrayToBean;
            default:
                throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_VALUE,
                        MongoDBReaderErrorCode.ILLEGAL_VALUE.getDescription());
        }
    }

    public static JSONObject parseObject(String jsonString, Feature f) {
        if (Objects.isNull(f)) {
            return JSON.parseObject(jsonString);
        } else {
            return JSON.parseObject(jsonString, f);
        }
    }
}
